package com.zhou.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhou.entity.Bills;
import com.zhou.service.BillsService;
import com.zhou.vo.RespData;
import com.zhou.vo.UpBillReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 服务控制器
 *
 * @author jilin
 * @since 2021-11-17 22:47:37
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RestController
@RequestMapping("/bill")
public class BillsController {
    @Autowired
    BillsService billsService;
    @GetMapping("/list")
    public RespData qryBillList() {
        return RespData.of(billsService.qryBillList());
    }
    @PostMapping("/create")
    public RespData createBill(@RequestBody Map<String, String> name) {
        try {
            billsService.createBill(name.get("name"));
        } catch (Exception e) {
            return RespData.bad(e.getMessage());
        }
        return RespData.ok();
    }
    @PostMapping("edit")
    public RespData updateBill(@RequestBody UpBillReq req) {
        log.info("updatebill method param:" + req);
        try {
            billsService.updateBill(req.getId(), req.getName());
        } catch (Exception e) {
            return RespData.bad(e.getMessage());
        }
        return RespData.ok();
    }
    @PostMapping("delete")
    public RespData deleteBill(@RequestBody int id) {
        log.info("deletebill method params: " + id);
        billsService.deleteBill(id);
        return RespData.ok();
    }
}